$(document).ready(
				function() {
					var contextPath = document.location.pathname; 
					var index =contextPath.substr(1).indexOf("/"); 
					contextPath = contextPath.substr(1,index); 
					var basePath="/"+contextPath;
					var totalCount = $("#totalCount").val();
					var pSize=$("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					$("#memberDetails").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn")
							.delegate(
									'#saveBtn','click',
									function() {
										
										$("#memberInfo").attr("method","post");
										
										var memberId = $("#memberId").val();
											
											if (memberId =="") {
												 $("#memberInfo").attr("action","save.do");
											} else {
												
												 $("#memberInfo").attr("action","update.do");
											}
											$("#memberInfo").submit();
										
									});

					$('#pagination2').jqPaginator(
									{
										 totalPages: pageCount,
										    visiblePages: 10,
										    currentPage: 1,
										
										    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex :num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															
															$("#memberList tr:not(:first)").remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var member = results[i];
																
																var rowContent = "<tr>"
																		+ "<td><input name='memberId' class='checkbox' type='checkbox' value='"
																		+ member.id
																		+ "'/></td>"
																		+ "<td>"
																		+ member.id
																		+ "</td>"
																		+ "<td>"
																		+ member.mbrName
																		+ "</td>"
																		+ "<td>"
																		+ member.mail
																		+ "</td>"
																		+ "<td>"
																		+ member.mobile
																		+ "</td>"
																		+ "<td>"
																		+ member.born
																		+ "</td>"
																		+ "<td>"
																		+ member.age
																		+ "</td>"
																		+ "<td>";
																if(member.sex==0){
																	rowContent = rowContent
																	+ "男";
																}else {
																	rowContent = rowContent
																	+ "女";
																}
																if (member.statu == 0) {
																	rowContent = rowContent+"<td>"
																	+ "正常"+"</td>";
																} else {
																	rowContent = rowContent+"<td>"
																			+ "<font color='red'>已锁定</font>"+"</td>";
																}

																rowContent = rowContent
																		+ "</td>";
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ member.id
																		+ "' class='btn btn-sm-block'>修改</button>"
																		/*+ "<button name='addOrder' objId='"
																		+ member.id
																		+ "' class='btn btn-sm-block'>添加订单</button></td>"*/
																		+ "</tr>";
																$("#memberList")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#memberList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='memberId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#memberList").delegate('[name="editBtn"]', 'click', function() {
						var memberId = $(this).attr("objId");
						var editUrl = "show.do?memberId=" + memberId;
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : memberId
							},
							cache : false,

							success : function(data) {
								loadMember(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
					/*$("#memberList").delegate('[name="addOrder"]', 'click', function() {
						var orderId = $(this).attr("objId");
						alert("orderId:"+orderId);
						var editUrl = "show.do?orderId=" + orderId;
						$.ajax({
							
							url : editUrl,
							dataType : 'json',
							data : {
								id : orderId
							},
							cache : false,
							
							success : function(data) {
								loadOrder(data);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});
						
					});
*/
					$("#tools")
							.delegate(
									'#deleteBtn',
									'click',
									function() {
										delChk = $('input[name="memberId"]:checked');

										if (delChk.length > 0) {
											if (confirm("Are you sure delete the record?")) {

												var ids = "";
												var delUrl = "delete.do";
                                                var idArr=new Array();
                                               
                                                
                                                for (var i = 0; i < delChk.length; i++) {
                                                	idArr.push(delChk.get(i).value);
                                           
											}
												
												$.ajax({
															url : delUrl,
															type: "POST",
															data : {
																ids : idArr.toString()
															},
															cache : false,

															success : function(
																	html) {
																alert("Delete successfully");
																refresh();
															},
															error : function(
																	html) {
																alert("Delete failly");
																refresh();
															}
														});
											} else {
												return;
											}
										} else {
											alert("Please select the record to delete");
											return;
										}
									});
					
					
					$("#tools").delegate('#addMember', 'click', function() {
						$("#memberDetails input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);
							

						});
						$("#sex-Male").attr("value","0");
						$("#sex-Female").attr("value","1");
						
						$("#memberDetails textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#memberDetails").modal('show');

					});
					
					
					

				});





function loadMember(member) {
   
	$("#memberId").val(member.id);
	$("#mbrName").val(member.mbrName);
	$("#mail").val(member.mail);
	$("#mobile").val(member.mobile);
	if(member.sex==0)
		{
		$("#sex-Male").attr("checked","checked");
		}
	if(member.sex==1)
	{
	$("#sex-Female").attr("checked","checked");
	}
	$("#age").val(member.age);
	$("#born").val(member.born);
	$("#statu").val(member.statu);
	$("#note").val(member.note);
	$("#memberDetails").modal('show');
}
/*function loadOrder(member) {
	$("#memberId").val(member.id);
	$("#memberName").val(member.mbrName);
	
	
	$("#orderDetails").modal('show');
}*/
function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
